<?php
namespace App\Domain;

use App\Model\Goods as ModelGoods;

class Goods {

    //按条件获取商品
    public function getGoods( $condition, $str, $like, $curpage ) {
        $model = new ModelGoods();
        $all_data = array();
        //获取总数量，分页使用
        $count = $model->getGoodsByConNum( $condition, $str, $like );
        if( $count <= ($curpage * 10) ){
            $all_data['hasmore'] = false;
        }else{
            $all_data['hasmore'] = true;
        }
        $curpage = ($curpage * 10) - 10;
        $all_data['data'] =  $model->getGoodsByCon( $condition, $str, $curpage, $like );
        //根据商品id查询具体的规格价格
        foreach ( $all_data['data'] as $k => &$v ){
            $goods_spec = $model->getGoodsSpec( $v['goods_id'] );
            foreach ( $goods_spec as $k_s => &$v_s ){
                $data = array();
                $data['name'] = '';
                $spec_item_id = explode( '_', $v_s['key'] );
                foreach ( $spec_item_id as $k_item => $v_item ){
                    $result = $model->getGoodsSpecItem( $v_item );
                    $data['name'] = $data['name'].' '.$result['item'];
                }
                $data['item_id'] = $v_s['item_id'];
                $data['price'] = $v_s['price'];
                $data['store_count'] = $v_s['store_count'];
                $data['spec_key'] = $v_s['key'];
                $v['spec'][] = $data;
            }
        }
        return $all_data;
    }

    //商品详情
    public function getGoodsDetail( $goods_id ) {
        $model = new ModelGoods();
        $goods_info =  $model->getGoodsDetail( $goods_id );
        if( $goods_info ){
            //根据返回的商品数据请求图片组
            $goods_images = $model->getGoodsImages( $goods_info['goods_id'] );
            foreach ( $goods_images as $k => $v ){
                $goods_info['images'][] = $v['image_url'];
            }
            //根据返回的商品数据请求评论
            $count = $model->getCommentsByConNum( $goods_id );
            $goods_info['comment_count'] = $count;
            $goods_comments = $model->getGoodsComment( $goods_info['goods_id'] );
            if( empty( $goods_comments ) ){
                $goods_info['comment'][] = array();
            }else{
                foreach ( $goods_comments as $k_c => &$v_c ){
                    $v_c['username'] = base64_decode( $v_c['username'] );
                    $v_c['img'] = explode(',', $v_c['img']);
                    $goods_info['comment'][] = $v_c;
                }
            }
            //根据返回的商品数据请求规格
            $goods_spec = $model->getGoodsSpec( $goods_info['goods_id'] );
            foreach ( $goods_spec as $k_s => &$v_s ){
                $data = array();
                $data['name'] = '';
                $spec_item_id = explode( '_', $v_s['key'] );
                foreach ( $spec_item_id as $k_item => $v_item ){
                    $result = $model->getGoodsSpecItem( $v_item );
                    $data['name'] = $data['name'].' '.$result['item'];
                }
                $data['item_id'] = $v_s['item_id'];
                $data['price'] = $v_s['price'];
                $data['store_count'] = $v_s['store_count'];
                $data['spec_key'] = $v_s['key'];
                $goods_info['spec'][] = $data;
            }
            //处理goods_content_img
            if( $goods_info['goods_content_img'] ){
                $goods_info['goods_content_img'] = explode( ',', $goods_info['goods_content_img'] );
            }else{
                $goods_info['goods_content_img'] = array();
            }
        }
        return $goods_info;
    }

    //添加购物车
    public function addCart( $data ){
        $model = new ModelGoods();
        //判断购物车是否有同样的商品
        $con = array();
        $con['user_id'] = $data['user_id'];
        $con['goods_id'] = $data['goods_id'];
        $con['goods_price'] = $data['goods_price'];
        $con['spec_key'] = $data['spec_key'];
        $res = $model->CartInfo( $con );
        //如果存在，在次基础上增加数量
        if( $res ){
            return $model->updateCartMsg( $data['user_id'], $res['id'], $data['goods_num'] );
        }else{
            return $model->addCart( $data );
        }
    }

    //购物车列表
    public function cartList( $user_id ){
        $model = new ModelGoods();
        $cart_info = $model->cartList( $user_id );
        //处理购物车规格+获取商品头图
        foreach ( $cart_info as $k => &$v ){
            $name = '';
            $spec_item_id = explode( '_', $v['spec_key'] );
            foreach ( $spec_item_id as $k_item => $v_item ){
                $result = $model->getGoodsSpecItem( $v_item );
                $name = $name.' '.$result['item'];
            }
            //获取图片
            $img = $model->getGoodsOriginalImg( $v['goods_id'] );
            $v['spec'] = $name;
            $v['images'] = $img['original_img'];
            $v['admin_id'] = $img['admin_id'];
        }
        return $cart_info;
    }

    //购物车删除
    public function cartDel( $cart_id ){
        $model = new ModelGoods();
        $data = explode( ',', $cart_id );
        foreach ( $data as $k => $v ){
            $res = $model->cartDel( $v );
            if( empty($res) ){
                return false;
            }
        }
        return 1;
    }

    //购物车列表进行添加删除
    public function ajaxCart( $user_id, $cart_id, $goods_num ){
        $model = new ModelGoods();
        return $model->updateCartMsg( $user_id, $cart_id, $goods_num );
    }

    //商品评论列表
    public function goodsComments( $goods_id, $curpage ){
        $model = new ModelGoods();
        $all_data = array();
        //获取总数量，分页使用
        $count = $model->getCommentsByConNum( $goods_id );
        if( $count <= ($curpage * 10) ){
            $all_data['hasmore'] = false;
        }else{
            $all_data['hasmore'] = true;
        }
        $curpage = ($curpage * 10) - 10;
        //根据返回的商品数据请求评论
        $goods_comments = $model->getGoodsComment( $goods_id, $curpage );
        foreach ( $goods_comments as $k_c => &$v_c ){
            $v_c['username'] = base64_decode( $v_c['username'] );
            $v_c['img'] = explode(',', $v_c['img']);
        }
        $all_data['data']['count'] =  $count;
        $all_data['data']['list'] =  $goods_comments;
        return $all_data;
    }
}
